package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.base.BaseActivity;
import com.Lixiaoqian.CardPlay.base.MyApp;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.DialogUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;

import static com.Lixiaoqian.CardPlay.base.MyApp.umShareAPI;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_login_in)
    Button btnLoginIn;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.im_right)
    ImageView imRight;
    @BindView(R.id.imageView1)
    ImageView imageView1;
    @BindView(R.id.top_login)
    RelativeLayout topLogin;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_mima)
    EditText editMima;
    @BindView(R.id.relativeLayout4)
    LinearLayout relativeLayout4;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.layout_weixin)
    RelativeLayout layoutWeixin;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.layout_sina)
    RelativeLayout layoutSina;
    private UMAuthListener umAuthListener;
    private ArrayList<EditText> editTexts  = new ArrayList<EditText>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initEvent();
    }

    private void initEvent() {
        btnLoginIn.setOnClickListener(this);
        tvForget.setOnClickListener(this);
        imRight.setOnClickListener(this);
    }

    @Override
    protected void initToolbar() {

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login;
    }


    public static void launch(Activity fromActivity) {
        fromActivity.startActivity(new Intent(fromActivity, LoginActivity.class));
    }

    /**
     * 第三方登录回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyApp.umShareAPI.onActivityResult(requestCode, resultCode, data);

    }

    /**
     * 调用第三方登录实现
     */
    public void AutoLogin(SHARE_MEDIA s) {
        umAuthListener = new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                DialogUtils.showCustomDialogFragment(LoginActivity.this);
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                DialogUtils.dismissProgress();
                Toast.makeText(LoginActivity.this, map.get("name"), Toast.LENGTH_SHORT).show();
                closeActivity();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                DialogUtils.dismissProgress();
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                DialogUtils.dismissProgress();
            }
        };
        umShareAPI.getPlatformInfo(this, s, umAuthListener);
    }

    public void autoClick(View v) {
        switch (v.getId()) {
            case R.id.layout_weixin:
                AutoLogin(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.layout_sina:
                AutoLogin(SHARE_MEDIA.SINA);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_forget:
                RegisterActivity.launch(this, true);
                break;
            case R.id.im_right:
                RegisterActivity.launch(this, false);
                break;
            case R.id.btn_login_in:
                editTexts.add(editPhone);
                editTexts.add(editMima);
                editEmpty(editTexts,"手机号或密码为空");
                break;
        }
    }

    public void closeActivity(){
        setResult(Constant.RESULTCODE);
        finish();
    }
}

